<?php
class BaidulbsService {

    const ak='BYCTRbzNiVXyLDqdPnlfqZAtlDNd5O9c';
    const geotable_id="146698";

    //搜索半径,单位米
    const radius='50000';

    //坐标种类   1：GPS经纬度坐标2：国测局加密经纬度坐标3：百度加密经纬度坐标4：百度加密墨卡托坐标
    const coord_type = '1';

    const pk_id = "worker_id"; //主键id


    const  Create_data = 'http://api.map.baidu.com/geodata/v3/poi/create';
    const  Update_data = 'http://api.map.baidu.com/geodata/v3/poi/update';
    const  Detele_data = 'http://api.map.baidu.com/geodata/v3/poi/delete';
    const  Get_data = 'http://api.map.baidu.com/geodata/v3/poi/detail';
    const  get_datalist = 'http://api.map.baidu.com/geosearch/v3/nearby?ak=';


    const GetCodeSuccess = 0; //返回正确的code




    /*
    *  更新工人信息
     *
    *
    */
    public static function upateWorkerLocation($lng,$lat,$workerid,$title,$tag)
    {
        $data = BaidulbsService::getdata($workerid);

        $poi = $data->poi;

        if($poi == null)
        {

            //插入
            BaidulbsService::createdata($lng,$lat,$workerid,$title,$tag);

        }
        else
        {
            //update

            BaidulbsService::updatedata($lng,$lat,$workerid,$title,$tag);

        }




    }

    /*
    *  获取百度云存储数据
    *
    */
    public static function getdata ($workerid)
    {

        $url =BaidulbsService::Get_data."?ak=".BaidulbsService::ak."&geotable_id=".BaidulbsService::geotable_id."&".BaidulbsService::pk_id."=".$workerid;

        $postarray = array();

        $data = Yii::app()->CURL->run($url,true, $postarray,TRUE,TRUE);

        $data = json_decode($data);


        $data = BaidulbsService::checkresponse($data);


     return  $data;



    }


    /*
    * 验证返回值
     *
    *
    */
    public static function checkresponse ($data)
    {
        if($data != null)
        {
            if(array_key_exists('status',  $data)==false)
            {
               // Util::echolog("百度访问请求失败 ");
                throw new Exception("百度访问请求失败");
            }
            else
            {
                $status = $data->status;

                if($status == BaidulbsService::GetCodeSuccess)
                {

                    return $data;

                }
                else
                {
                    //Util::echolog("百度访问请求失败 ".$data->message);
                    throw new Exception("百度访问请求失败 ".$data->message);
                }

            }
        }
        else
        {
            //Util::echolog("百度访问请求失败 ");
            throw new Exception("百度访问请求失败");
        }


    }


    //在百度lbs创建工人位置信息
    public static function createdata($lng,$lat,$workerid,$title,$tag){

        $url =BaidulbsService::Create_data;

        $postarray = array();
        $postarray['ak'] = BaidulbsService::ak;
        $postarray['geotable_id'] = BaidulbsService::geotable_id;
        $postarray['longitude'] = $lng;
        $postarray['latitude'] = $lat;
        $postarray['coord_type'] = BaidulbsService::coord_type;
        $postarray[BaidulbsService::pk_id] = $workerid;
        $postarray['title'] = $title;
        $postarray['tags'] = $tag;

        $data = Yii::app()->CURL->run($url,FALSE, $postarray,TRUE,TRUE);

        $data = json_decode($data);

        BaidulbsService::checkresponse($data);

        return $data;

    }


    //在百度lbs修改工人位置信息
    public static function updatedata($lng,$lat,$workerid,$title,$tag){

        $url =BaidulbsService::Update_data;



        $postarray = array();
        $postarray['ak'] = BaidulbsService::ak;
        $postarray['geotable_id'] = BaidulbsService::geotable_id;
        $postarray['longitude'] = $lng;
        $postarray['latitude'] = $lat;
        $postarray['coord_type'] = BaidulbsService::coord_type;
        $postarray[BaidulbsService::pk_id] = $workerid;
        $postarray['title'] = $title;
        $postarray['tags'] =$tag;



        $data = Yii::app()->CURL->run($url,FALSE, $postarray,TRUE,TRUE);

        $data = json_decode($data);



        BaidulbsService::checkresponse($data);

        return $data;

    }


    //在百度lbs删除工人位置信息
    public static function deteledata($workerid){

        $url =BaidulbsService::Detele_data;

        $postarray = array();
        $postarray['ak'] = BaidulbsService::ak;
        $postarray['geotable_id'] = BaidulbsService::geotable_id;
        $postarray[BaidulbsService::pk_id] = $workerid;

        $data = Yii::app()->CURL->run($url,FALSE, $postarray,TRUE,TRUE);

        $data = json_decode($data);

        BaidulbsService::checkresponse($data);

        return $data;

    }

    //在百度lbs查询附近工人id列表
    public static function nearlydata($lng,$lat,$page_index,$page_size,$tag){

        $location = $lng.','.$lat;

        $url =BaidulbsService::get_datalist;
        $url = $url.BaidulbsService::ak;
        $url = $url.'&geotable_id='.BaidulbsService::geotable_id;
        $url = $url.'&location='.$location;
        $url = $url.'&radius='.BaidulbsService::radius;
        $url = $url.'&tags='.$tag;

        if($page_index !=''){
            $url = $url.'&page_index='.$page_index;
        }
        if($page_size !=''){
            $url = $url.'&page_size='.$page_size;
        }

        $postarray = array();

        $data = Yii::app()->CURL->run($url,true, $postarray,TRUE,TRUE);

        $data = json_decode($data);

        return $data;

    }


}